# -*- coding: utf-8 -*-
# @Time : 2022/6/6 上午10:04
# @Author : wyf
# @FileName: 作业4.py
# @Email: yunfei.wang@innvote.com
import time

import requests
from flask import Flask, render_template
from gevent.pywsgi import WSGIServer


class ProxyApi(object):

    def __init__(self, url):
        self.url = url
        self.app = Flask(__name__)

        @self.app.route("/")
        def test():
            return "Hello Flask"

        @self.app.route('/index')
        def index():
            content = {
                'status': 0,
                'data': get_data(self.url)
            }
            # flask 框架 返回数据给前端
            return render_template('index4.html', data=content)

    def run(self):
        # self.app.run("0.0.0.0", 8006)
        server = WSGIServer(("0.0.0.0", 8006), self.app)
        server.serve_forever()


def get_data(url):
    res = requests.get(url)
    print(res.text)
    data_list = []
    try:
        data = res.json().get("data").get("rows")

    except Exception as e:
        print("解析返回数据错误")
        return data_list
    for item in data:
        notice_guid = item.get("noticeguid")
        source_guid = item.get("sourceguid")
        detail = f"https://hljcg.hlj.gov.cn/provide/publicity/detailFromIndex?noticeGuid={notice_guid}&sourceGuid={source_guid}"
        _info = {
            "title": item.get("title"),
            "publish": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(item.get("publishtime")//1000)),
            "region": item.get("regionName"),
            "detail": detail
        }
        data_list.append(_info)
    return data_list


if __name__ == '__main__':
    _url = "https://hljcg.hlj.gov.cn/gateway/gp-provide/api/v2/notice/searchProvideProjectNotice?noticeType=1&pageSize=10&pageNumber=1&_t=1654574287905"
    ProxyApi(_url).run()
